# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

from isbn.models import Book


class SpiderPipeline(object):
    def process_item(self, item, spider):
        isbn = item["uid"]
        if isbn is None:
            return

        try:
            book = Book.objects.get(uid=isbn)
            return book
        except Book.DoesNotExist:
            pass

        book = Book()
        book.uid = isbn
        book.name = item["name"]

        if 'publisher' in item:
            book.publisher = item["publisher"]

        if 'issue_date' in item:
            book.issue_date = item["issue_date"]

        if 'pages' in item:
            book.pages = item["pages"]

        if 'author_intro' in item:
            book.author_intro = item["author_intro"]

        if 'contents' in item:
            book.contents = item["contents"]

        if 'origin_name' in item:
            book.origin_name = item['origin_name']

        if 'price' in item:
            book.price = item["price"]

        if 'pkg' in item:
            book.pkg = item["pkg"]

        if 'agenda' in item:
            book.agenda = item['agenda']

        if 'authors' in item:
            book.authors = item['authors']

        if 'translators' in item:
            book.translators = item['translators']

        if 'image' in item:
            book.image = item['image']

        if 'title' in item:
            book.title = item['title']

        if 'producer' in item:
            book.producer = item['producer']

        if 'series' in item:
            book.series = item['series']

        book.save()
        return item
